import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, OnChanges } from '@angular/core';
import { NzTableQueryParams } from 'ng-zorro-antd/table';
import { txsInfoItemsEntity, ColumnItem, blockEntity, txInfoEntity, txRecipyEntity } from './message-details.entity';
import { MessageDetailsService } from './message-details.service';
@Component({
  selector: 'app-message-details',
  templateUrl: './message-details.component.html',
  styleUrls: ['./message-details.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MessageDetailsComponent implements OnInit, OnChanges {
  txInfo: txInfoEntity;
  txRecipy :txRecipyEntity;

  isVisible: boolean = false;
  pageIndex: number;
  pageSize: number;

  loading: boolean = false;
  txsInfoItems: txsInfoItemsEntity[] = [];
  txsInfoTableTab: string[] = ['12%', '34%', '25%', '25%', '4%'];
  blockNumber: number;
  transactionhash: blockEntity[] = [];
  listOfColumns: ColumnItem[] = [
    {
      name: '时间',
      sortOrder: null,
      sortFn: (a: txsInfoItemsEntity, b: txsInfoItemsEntity) => a.time.localeCompare(b.time),
    }
  ];
  constructor(
    private readonly messageDetailsService: MessageDetailsService,
    private ref: ChangeDetectorRef
  ) { }

  loadDataFromServer(): void {
    this.txsInfoItems = [];
    this.messageDetailsService.getBlockNumber().subscribe(num => {
      this.blockNumber = num;
      for (let i = num; i >= 1; --i) {
        this.messageDetailsService.getBlockDetails(i).subscribe(obj => {
          let date = new Date(obj.timestamp);
          let Y = date.getFullYear() + '-';
          let M = (date.getMonth()+1 < 10 ? '0'+ (date.getMonth()+1) : date.getMonth()+1) + '-';
          let D = (date.getDate() < 10 ? '0'+ (date.getDate()) : date.getDate()) + ' ';
          let h = (date.getHours() < 10 ? '0'+ (date.getHours() ) : date.getHours() ) + ':';
          let m = (date.getMinutes() < 10 ? '0'+ (date.getMinutes() ) : date.getMinutes() ) + ':';
          let s = (date.getSeconds() < 10 ? '0'+ (date.getSeconds() ) : date.getSeconds() ); 
          console.log(Y+M+D+h+m+s);
          this.txsInfoItems.push({
            time: Y+M+D+h+m+s,
            from: obj.transactions[0].from,
            to: obj.transactions[0].to,
            hash: obj.transactions[0].hash,
          });
          this.txsInfoItems = [...this.txsInfoItems];
          this.ref.detectChanges();
        })
      }
    });
  }

  loadRecipyAndInfo(hash: string): void {

  }
   
  judge_change(): void {
    if (this.blockNumber <= 0) {
      this.loadDataFromServer();
    }
  }

  ngOnInit(): void {
    this.txsInfoItems = this.txsInfoItems || [];
    this.loadDataFromServer();
    // this.txsInfoItems.push({
    //   time: 'Y+M+D+h+m+s',
    //   from: 'obj.transactions[0].from',
    //   to: 'obj.transactions[0].to',
    //   hash: 'obj.transactions[0].hash',
    // });
    // this.txsInfoItems = [...this.txsInfoItems];
    // this.ref.detectChanges();
  }

  ngOnChanges(): void {
  }

  showModal(hash: string) {
    this.isVisible = true;
    this.messageDetailsService.getTxInfo(hash).subscribe(str => {
      this.txInfo = str;
      this.ref.detectChanges();
      console.log(this.txInfo);
    });
    this.messageDetailsService.getTxRecipy(hash).subscribe(str => {
      this.txRecipy = str;
      this.ref.detectChanges();
      console.log(this.txRecipy);
    });
  }

  handleSettingCancel(): void {
    this.isVisible = false;
  }

  handleSettingOk(): void {
    this.isVisible = false;
  }
}
